# -*- coding: utf-8 -*-
from sklearn import datasets
from sklearn.linear_model import Perceptron
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score


def sk_perceptron():
    iris = datasets.load_iris()
    X = iris.data[:, [2, 3]]
    y = iris.target

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y)

    scaler = StandardScaler()
    scaler.fit(X_train)
    X_train_scaler = scaler.transform(X_train)
    X_test_scaler = scaler.transform(X_test)

    perceptron = Perceptron(n_iter_no_change=40, eta0=0.1, random_state=1)
    perceptron.fit(X_train_scaler, y_train)
    y_predict = perceptron.predict(X_test_scaler)

    # 准确率
    accuracy = accuracy_score(y_predict, y_test)
    print('accuracy:%.4f' % accuracy)


if __name__ == '__main__':
    sk_perceptron()

    pass
